#include <stdio.h>

#include "CDSF_AppInit.h"
#include "CDSF_Tasks.h"

#define kTaskTableSize	32

static SFTask m_tasks[kTaskTableSize];

static SFTaskTable m_taskTable = {
	.tasks = m_tasks,
	.count = 0,
};

static bool m_appStarted = FALSE;

SFCode SFAppAddTask(SFTaskType type, char* name, int rate, int priority, int stackSize, SFTaskInitFunction init, SFTaskDelegateFunction delegate){
	SFTask *task;
	
	if(m_appStarted){
		return SFCodeError;
	}
	
	if((m_taskTable.count)>=kTaskTableSize){
		return SFCodeSpaceFullError;
	}
	
	task = &(m_taskTable.tasks[m_taskTable.count]);
	task->type = type;
	task->name = name;
	task->rate = rate;
	task->priority = priority;
	task->stackSize = stackSize;
	task->init = init;
	task->delegate = delegate;
	task->counter = 0;
	
	++(m_taskTable.count);
	
	return SFCodeOK;
};

SFCode SFAppStart(int sysClockRate, int auxClockRate){
	SFCode rt;
	
	rt = SFAppInitHardware();
	
	if(SFOK(rt)){
		rt = SFInitTasks(&m_taskTable, auxClockRate);
	}
	
	if(SFOK(rt)){
		rt = SFAppInitClocks(sysClockRate, auxClockRate);
	}
	
	if(SFOK(rt)){
		rt = SFStartTasks();
	}
	
	if(SFError(rt)){
		return rt;
	}
	
	m_appStarted = TRUE;
	
	return SFCodeOK;
}
